home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-games-data / glchess / uci.py < prev   
Encoding:
Python Source  |  2009-04-14  |  5.2 KB  |  193 lines

  1. # -*- coding: utf-8 -*-
  2.  
  3. class StateMachine:
  4.     """
  5.     """
  6.     
  7.     STATE_IDLE = 'IDLE'
  8.     STATE_CONNECTING = 'CONNECTING'
  9.     
  10.     options = None
  11.     
  12.     __positionCommand = 'position startpos'
  13.     __haveMoves = False
  14.     
  15.     buffer = ''
  16.     
  17.     __readyToConfigure = False
  18.     __options          = None
  19.     
  20.     __ready            = False
  21.     __inCallback       = False
  22.     __queuedCommands   = None
  23.     
  24.     def __init__(self):
  25.         """
  26.         """
  27.         self.options = {}
  28.         self.__queuedCommands = []
  29.         
  30.     def logText(self, text, style):
  31.         """
  32.         """
  33.         pass
  34.         
  35.     def onOutgoingData(self, data):
  36.         """
  37.         """
  38.         pass
  39.  
  40.     def onMove(self, move):
  41.         """Called when the AI makes a move.
  42.         
  43.         'move' is the move the AI has decided to make (string).
  44.         """
  45.         print 'UCI move: ' + move
  46.         
  47.     def registerIncomingData(self, data):
  48.         """
  49.         """
  50.         self.__inCallback = True
  51.         self.buffer += data
  52.         while True:
  53.             index = self.buffer.find('\n')
  54.             if index < 0:
  55.                 break
  56.             line = self.buffer[:index]
  57.             self.buffer = self.buffer[index + 1:]
  58.             self.parseLine(line)
  59.         self.__inCallback = False
  60.         
  61.         if self.__options is not None and self.__readyToConfigure:
  62.             options = self.__options
  63.             self.__options = None
  64.             self.configure(options)
  65.  
  66.         # Send queued commands once have OK
  67.         if len(self.__queuedCommands) > 0 and self.__ready:
  68.             commands = self.__queuedCommands
  69.             self.__queuedCommands = []
  70.             for c in commands:
  71.                 self.__sendCommand(c)
  72.                 
  73.     def __sendCommand(self, command):
  74.         """
  75.         """
  76.         if self.__ready and not self.__inCallback:
  77.             self.onOutgoingData(command + '\n')
  78.         else:
  79.             self.__queuedCommands.append(command)
  80.  
  81.     def start(self):
  82.         """
  83.         """
  84.         self.onOutgoingData('uci\n')
  85.         
  86.     def startGame(self):
  87.         """
  88.         """
  89.         self.__sendCommand('ucinewgame')
  90.         self.__sendCommand(self.__positionCommand)
  91.  
  92.     def configure(self, options = []):
  93.         """
  94.         """
  95.         if not self.__readyToConfigure:
  96.             self.__options = options
  97.             return
  98.  
  99.         for option in options:
  100.             if not hasattr(option, 'name'):
  101.                 print 'Ignoring unnamed UCI option'
  102.                 continue
  103.             if option.value == '':
  104.                 continue
  105.             self.onOutgoingData('setoption ' + option.name + ' value ' + option.value + '\n')
  106.         self.onOutgoingData('isready\n')
  107.  
  108.     def requestMove(self, whiteTime, blackTime, ownTime):
  109.         """
  110.         """
  111.         # Some AI's don't work unless assigned some time
  112.         # TODO: which ones? I think Glaurung had issues
  113.         if whiteTime == 0:
  114.             whiteTime = 30000
  115.         if blackTime == 0:
  116.             blackTime = 30000
  117.             
  118.         self.__sendCommand('go wtime %d btime %d' % (whiteTime, blackTime))
  119.         
  120.     def undoMove(self):
  121.         """
  122.         """
  123.         self.__sendCommand('stop');
  124.         (self.__positionCommand, _) = self.__positionCommand.rsplit(" ", 1)
  125.         if self.__positionCommand.endswith(' moves'):
  126.             self.__haveMoves = False
  127.             self.__positionCommand = 'position startpos'
  128.         self.__sendCommand(self.__positionCommand)        
  129.  
  130.     def reportMove(self, move, isSelf):
  131.         """
  132.         """
  133.         if not self.__haveMoves:
  134.             self.__positionCommand += ' moves'
  135.         self.__haveMoves = True
  136.         self.__positionCommand += ' ' + move
  137.         self.__sendCommand(self.__positionCommand)
  138.  
  139.     def parseLine(self, line):
  140.         """
  141.         """
  142.         words = line.split()
  143.         
  144.         while True:
  145.             if len(words) == 0:
  146.                 self.logText(line + '\n', 'input')
  147.                 return
  148.             
  149.             style = self.parseCommand(words[0], words[1:])
  150.             if style is not None:
  151.                 self.logText(line + '\n', style)
  152.                 return
  153.  
  154.             print 'WARNING: Unknown command: ' + repr(words[0])
  155.             words = words[1:]
  156.  
  157.     def parseCommand(self, command, args):
  158.         """
  159.         """
  160.         if command == 'id':
  161.             return 'info'
  162.         
  163.         elif command == 'uciok':
  164.             if len(args) != 0:
  165.                 print 'WARNING: Arguments on uciok: ' + str(args)
  166.             self.__readyToConfigure = True
  167.             return 'info'
  168.         
  169.         elif command == 'readyok':
  170.             if len(args) != 0:
  171.                 print 'WARNING: Arguments on readyok: ' + str(args)
  172.             self.__ready = True
  173.             return 'info'
  174.         
  175.         elif command == 'bestmove':
  176.             if len(args) == 0:
  177.                 print 'WARNING: No move with bestmove'
  178.                 return 'error'
  179.             else:
  180.                 move = args[0]
  181.                 self.onMove(move)
  182.                 
  183.                 # TODO: Check for additional ponder information
  184.                 return 'move'
  185.         
  186.         elif command == 'info':
  187.             return 'info'
  188.         
  189.         elif command == 'option':
  190.             return 'info'
  191.         
  192.         return None
  193.